var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    shopInfo: null,
    type: 0,
    searchValue: '',
    receiptList: [],
    page: 1,
    limit: 10,
    totalPages: 1,
    hasLogin: false
  },
  init: function () {
    this.setData({
      receiptList: [],
      page: 1,
      limit: 10,
      totalPages: 1
    });
  },
  switchTab: function (event) {
    let type = event.currentTarget.dataset.index;
    this.setData({
      type: type
    });
    this.init();
    this.getReceiptList();
  },
  getReceiptList(reachBottom = false) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.ReceiptList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      searchValue: that.data.searchValue
    }).then(function (res) {
      if (res.errno === 0) {
        let receiptList = res.data.list.map(item => {
          item.remarkTime = util.formatTime2(new Date(item.remarkTime), "Y/M/D h:m:s");
          item.remarkImage && (item.remarkImage = item.remarkImage.split(',')[0]);
          // item.remarkImage = item.remarkImage || '/static/images/receipt_item.png';
          return item;
        });
        //触底加载新数据并保留老数据
        if (reachBottom) {
          receiptList = [...that.data.receiptList, ...receiptList]
        }
        that.setData({
          receiptList: receiptList,
          totalPages: res.data.pages
        });

        wx.hideLoading();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type
      });
    }
  },
  onReady: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.init();
    this.getReceiptList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
    } else {
      return false;
    }
    this.getReceiptList(true);
  },
  onShow: function () {
    if (app.globalData.hasLogin) {
      
      let shopInfo = wx.getStorageSync('shopInfo');
      this.setData({
        shopInfo: shopInfo
      });

      this.init();
      this.getReceiptList();
    }
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
  //采购明细
  openReceipt(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let item = this.data.receiptList[index];

    wx.navigateTo({
      url: `/pages/ucenter/receiptDetail/receiptDetail?id=${item.id}&itemId=${item.itemId}`
    });
  },
  //按下事件开始  
  touchStart: function (e) {
    let that = this;
    that.setData({
      touchStart: e.timeStamp
    })
  },
  //按下事件结束  
  touchEnd: function (e) {
    let that = this;
    that.setData({
      touchEnd: e.timeStamp
    })
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  onSearch() {
    this.getReceiptList();
  },
})